<?php

require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_bind extends fields_common
{
   public $articles   = FALSE;
   public $categories = FALSE;
   public $independed = TRUE;
   public $types      = FALSE;

   public function value($lang = NULL)
   {
   }

   /**
    * Normalize Value
    *
    * @param mixed $data
    * @param string $alias
    * @param array $article
    */
   public function normalizeValue()
   {
   }


   public function validateInput($value, $dataObject)
   {
      if(empty($dataObject->id)) return FALSE;

      $DB = database::getInstance();
      $node_belong = get_class($dataObject) == 'article' ? 'a' : 'c';

      $sql = 'DELETE FROM `meccano_binds` WHERE `node_belong` = "'. $node_belong .'" AND `node` = '. $dataObject->id;
      $DB->sql = $sql;
      $DB->DB_Query();
      
      unset($value['delete']);

      if(is_array($value) && !empty($value))
      {
         $tmp = array();
         foreach ($value AS $k => $v)
         {
            $tmp[] = ($k < 0 ? 'NULL' : $k) . ', "'.
               $node_belong .'", ' .
               $v['position'] . ', ' .
               $dataObject->id . ', ' .
               $v['bind'] . ', "' .
               $v['bind_belong'] . '", '.
               (!empty($v['type']) ? '"'. implode(',', array_filter((array)$v['type'], 'is_numeric')) .'"' : ' NULL');
         }
         $sql = 'INSERT INTO `meccano_binds` (`id`, `node_belong`, `position`, `node`, `bind`, `bind_belong`, `types`) VALUES (' . implode('),(', $tmp) . ')';
         $DB->sql = $sql;
         $DB->DB_Query();
      }
      return TRUE;
   }


   public function saveValue($lang = NULL)
   {
   }


   /**
    * Get HTML code for Logic fields
    * @param object $data
    * @return string
    */
   public function getFieldHTML($data)
   {
      $value = $data->{$this->alias}(null);
      $KERNEL = kernel::getInstance();


      $html  = '<div id="f_' . $this->alias . '">' . EOL;
      $html .= '<input type="hidden" name="f['. $this->alias .'][delete]" value="1" />' . EOL;
      if (empty($value) || !is_array($value))
      {
         $html .= '<div id="f_' . $this->alias . '_epsent">'. gettext::translate('meccano', 'epsent_tied_material') .'</div>' . EOL;
      } else
      {
         $data = array();
         if(!empty($value['categories'])) $data = array_merge($data, $value['categories']);
         if(!empty($value['articles']))   $data = array_merge($data, $value['articles']);
         if(!empty($data))
         {
            foreach ($data AS $c)
            {
               $html .= '<div class="fieldset" id="f_'. $this->alias .'_'. $c->getMeta('bind') .'">'. EOL;
               $html .= '<input name="f['. $this->alias .']['. $c->getMeta('id') .'][position]" value="'. $c->getMeta('position') .'" rel="position" type="hidden">'. EOL;
               $html .= '<input name="f['. $this->alias .']['. $c->getMeta('id') .'][bind]" value="'. $c->getMeta('bind') .'" type="hidden" />'. EOL;
               $html .= '<input name="f['. $this->alias .']['. $c->getMeta('id') .'][bind_belong]" value="'. $c->getMeta('bind_belong') .'" type="hidden" />'. EOL;
               $html .= '<img src="/tpa/img/16/' . ($c->getMeta('bind_belong') == 'c' ? 'folders_arrow.png' : 'document_arrow.png') . '" width="16" height="16" style="margin-right: 15px;" />'. EOL;
               $html .= $c->title;
               $html .= '<div class="fl-right">'. EOL;
               $html .= '<a href="javascript:void(0)" onclick="$(this).parent().parent().remove();" class="a_icon i_delete">'. gettext::translate('meccano', 'delete') .'</a>';
               $html .= '<a class="moveable24">'. gettext::translate('meccano', 'move') .'</a></div>'.EOL;
               if($this->types)
               {
                  $types = $KERNEL->getTypes('b');
                  if($types)
                  {
                     $html .= '<div class="fl-right bind-types"><ul>';
                     foreach($types AS $t=>$v)
                     {
                        $html .= '<li><input type="checkbox" name="f['. $this->alias .']['. $c->getMeta('id') .'][type][]" value="'. $t .'"';
                        if(in_array($t, (array)$c->getMeta('types'))) $html .= ' checked="checked"';
                        $html .= ' id="f_'. $this->alias .'_'. $c->getMeta('id') .'_type_'. $t .'" />';
                        $html .= ' - <label for="f_'. $this->alias .'_'. $c->getMeta('id') .'_type_'. $t .'">'. $v .'</label></li>';
                     }
                     $html .= '</li></div>'.EOL;
                  }
               }
               $html .= '<div class="clear"></div></div>'. EOL;
            }
         }
      }
      $html .= '</div>';

      $html .= '<script type="text/javascript">'. EOL;
      $html .= 'var '. $this->alias .'_id = 1;'. EOL;
      $html .= 'function AddInField_'. $this->alias .'(node)'. EOL;
      $html .= '{'. EOL;
      $html .= 'if($("#f_'. $this->alias .'_"+node.id).size() > 0) { alert("'. htmlspecialchars(gettext::translate('meccano', 'tied_article_present')) .'"); return false; }'. EOL;
      $html .= 'if($("#f_'. $this->alias .'_epsent").size() > 0) $("#f_'. $this->alias .'_epsent").remove();'. EOL;
      $html .= 'var html = \'<div class="fieldset" id="f_' . $this->alias . '_\'+node.id+\'">\';'. EOL;
      $html .= 'html += \'<input name="f['. $this->alias .'][-\'+'. $this->alias .'_id+\'][position]" value="" rel="position" type="hidden">\';'. EOL;
      $html .= 'html += \'<input name="f['. $this->alias .'][-\'+'. $this->alias .'_id+\'][bind]" value="\'+node.id+\'" type="hidden" />\';'. EOL;
      $html .= 'html += \'<input name="f['. $this->alias .'][-\'+'. $this->alias .'_id+\'][bind_belong]" value="\'+node.folder+\'" type="hidden" />\';'. EOL;
      $html .= 'html += \'<img src="/tpa/img/16/\'+(node.folder == "c" ? "folders_arrow.png" : "document_arrow.png")+\'" width="16" height="16" style="margin-right: 15px;" />\';'. EOL;
      $html .= 'html += node.title;'. EOL;
      $html .= 'html += \'<div class="fl-right">\';'. EOL;
      $html .= 'html += \'<a href="javascript:void(0)" onclick="$(this).parent().parent().remove();" class="a_icon i_delete">'. htmlspecialchars(gettext::translate('meccano', 'delete')) .'</a>\';'. EOL;
      $html .= 'html += \'<a class="moveable24">'. htmlspecialchars(gettext::translate('meccano', 'move')) .'</a></div>\';'. EOL;

      if($this->types)
      {
         $types = $KERNEL->getTypes('b');
         if($types)
         {
            $html .= 'html += \'<div class="fl-right bind-types"><ul>\';';
            foreach($types AS $t=>$v)
            {
               $html .= 'html += \'<li><input type="checkbox" name="f['. $this->alias .'][-\'+'. $this->alias .'_id+\'][type][]" value="'. $t .'"\';';
               $html .= 'html += \' id="f_'. $this->alias .'_-\'+' . $this->alias . '_id+\'_type_'. $t .'" />\';';
               $html .= 'html += \' - <label for="f_'. $this->alias .'_-\'+' . $this->alias . '_id+\'_type_'. $t .'">'. $v .'</label></li>\';';
            }
            $html .= 'html += \'</li></div>\';'.EOL;
         }
      }

      $html .= 'html += \'<div class="clear"></div>\';' . EOL;
      $html .= 'html += \'</div>\';' . EOL;
      $html .= '$(\'#f_' . $this->alias . '\').append(html);' . EOL;
      $html .= $this->alias . '_id++;' . EOL;
      $html .= '}' . EOL;
      $html .= '$(\'#f_' . $this->alias . '\').sortable({\'items\':\'>div\',\'handle\':\'.moveable24\',\'revert\':true,\'update\':ResortFieldset});' . EOL;
      $html .= '</script>' . EOL;

      return $html;
   }
}